#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>

//void qsort(void* base, size_t num, size_t size,
//		     int(*compar)(const void*, const void*));

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}


int IntCmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

void Swap(void* p1, void* p2, int size)
{
	for (int i = 0; i < size; i++)
	{
		char tmp = *(char*)p1;
		*(char*)p1 = *(char*)p2;
		*(char*)p2 = tmp;
		
		p1 = (char*)p1 + 1;
		p2 = (char*)p2 + 1;
	}
}

void MyQsort(void* base, size_t num, size_t size,
	    int(*compar)(const void*, const void*))
{
	for (int i = 0; i < num - 1; i++)
	{
		for (int j = 0; j < num - 1 - i; j++)
		{
			if (compar((char*)base + j*size, (char*)base + (j + 1)*size)>0)
			{
				Swap((char*)base + j*size, (char*)base + (j + 1)*size, size);
			}
		}
	}
}

void main()
{
	int a[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
	int sz = sizeof(a) / sizeof(a[0]);
	MyQsort(a, sz, sizeof(int), IntCmp);
	PrintArray(a, sz);
}


//void main()
//{
//	int arr1[] = { 1, 2, 3 };
//	int arr2[] = { 4, 5, 6 };
//	int arr3[] = { 7, 8, 9 };
//	int* a[] = { arr1, arr2, arr3 };
//	int** p = a;
//	int* (*pa)[3] = &a;
//	printf("%d\n", *(*(p + 2) + 2));
//	printf("%d\n", *(*((*pa) + 1) + 1));
//}